from django.http import JsonResponse
from django.shortcuts import render,HttpResponse

# Create your views here.
from django.views import View

from employee.models import *
from utils import loadaddr


class ListView(View):
    def get(self,request):
        employees = Employee.objects.all()
        return render(request,'admin-staffmanage.html',{'employees':employees})

class AddView(View):
    def get(self,request):
        ments = Ment.objects.all()
        len = [i for i in range(18,61)]
        provinces = loadaddr.get_provinces()
        return render(request,'as-insert.html',{'ments':ments,'len':len,"provinces":provinces})

    def post(self,request):
        s_name = request.POST.get('s_name')
        s_id = request.POST.get('s_id')
        sex = request.POST.get('sex')
        age = request.POST.get('age')
        ment = request.POST.get('ment')
        m = Ment.objects.get(ment_name=ment)
        phone = request.POST.get('phone')
        email = request.POST.get('email')
        position = request.POST.get('position')
        provinces = request.POST.get('provinces')
        citys = request.POST.get('citys')
        areas = request.POST.get('areas')

        pp = loadaddr.get_provinces()
        if citys != '':
            cc, aa = loadaddr.get_citys_areas_by_provinceid(provinces)
            aa = loadaddr.get_areas_by_cityid(citys)
            for p in pp:
                if p['id'] == provinces:
                    provinces = p['name']
                    break
            for c in cc:
                if c['id'] == citys:
                    citys = c['name']
                    break
            for a in aa:
                if a['id'] == areas:
                    areas = a['name']
                    break
            address = provinces + ',' + citys + ',' + areas
        else:
            cc, aa = loadaddr.get_citys_areas_by_provinceid(provinces)
            for p in pp:
                if p['id'] == provinces:
                    provinces = p['name']
                    break
            for a in aa:
                if a['id'] == areas:
                    areas = a['name']
                    break
            address = provinces + ',' + areas

        pwd ='123456'
        birthday = request.POST.get('birthday')

        emp = Employee(s_name=s_name,s_id=s_id,sex=sex,dizhi=address,password=pwd,birthday=birthday,age=age,phone=phone,ment=m,email=email,position=position)
        emp.save()

        employees = Employee.objects.all()
        return render(request,'admin-staffmanage.html',{'employees':employees})

class AddressView(View):
    def post(self,request):
        type = request.POST.get('type', '1')
        if type == '1':
            privinceId = request.POST.get('provinceId', '110000')
            citys, areas = loadaddr.get_citys_areas_by_provinceid(privinceId)
            return JsonResponse({'citys': citys, 'areas': areas})
        elif type == '2':
            cityId = request.POST.get('cityId', '')
            areas = loadaddr.get_areas_by_cityid(cityId)
            return JsonResponse({'areas': areas})

class DelView(View):
    def get(self,request):
        id =request.GET.get('id')
        delemp = Employee.objects.filter(s_id=id)
        delemp.delete()
        employees = Employee.objects.all()
        return render(request, 'admin-staffmanage.html', {'employees': employees})

class UpdateView(View):
    def get(self,request):
        id = request.GET.get('id')
        emp = Employee.objects.get(s_id=id)
        len = [i for i in range(18, 61)]
        ments = Ment.objects.all()
        provinces = loadaddr.get_provinces()
        return render(request,'as-update.html',{'emp':emp,'len':len,'ments':ments,"provinces":provinces})
    def post(self,request):
        s_name = request.POST.get('s_name')
        s_id = request.POST.get('s_id')
        sex = request.POST.get('sex')
        age = request.POST.get('age')
        ment = request.POST.get('ment')
        m = Ment.objects.get(ment_name=ment)
        phone = request.POST.get('phone')
        email = request.POST.get('email')
        position = request.POST.get('position')
        birthday = request.POST.get('birthday')
        provinces = request.POST.get('provinces')
        citys = request.POST.get('citys')
        areas = request.POST.get('areas')

        pp = loadaddr.get_provinces()
        if citys != '':
            cc, aa = loadaddr.get_citys_areas_by_provinceid(provinces)
            aa = loadaddr.get_areas_by_cityid(citys)
            for p in pp:
                if p['id'] == provinces:
                    provinces = p['name']
                    break
            for c in cc:
                if c['id'] == citys:
                    citys = c['name']
                    break
            for a in aa:
                if a['id'] == areas:
                    areas = a['name']
                    break
            address = provinces + ',' + citys + ',' + areas
        else:
            cc, aa = loadaddr.get_citys_areas_by_provinceid(provinces)
            for p in pp:
                if p['id'] == provinces:
                    provinces = p['name']
                    break
            for a in aa:
                if a['id'] == areas:
                    areas = a['name']
                    break
            address = provinces + ',' + areas

        Employee.objects.filter(s_id=s_id).update(s_name=s_name,dizhi=address,birthday=birthday,s_id=s_id,sex=sex,age=age,phone=phone,ment=m,email=email,position=position)
        employees = Employee.objects.all()

        return render(request,'admin-staffmanage.html',{"employees":employees,'succe':'修改成功'})


class ShowView(View):
    def get(self,request):
        id = request.GET.get('id')
        emp = Employee.objects.get(s_id=id)
        return render(request, 'personal-show.html', {'emp': emp})

class MohuView(View):
    def post(self,request):
        condition = request.POST.get('condition')
        mohu = request.POST.get('mohu')
        if condition=='1':
            if mohu == '':
                employees = Employee.objects.all()
                return render(request, 'admin-staffmanage.html', {"employees": employees})
            else:
                employees = Employee.objects.filter(s_name__contains=mohu)
                return render(request, 'admin-staffmanage.html', {"employees": employees})
        if condition=='2':
            if mohu == '':
                employees = Employee.objects.all()
                return render(request, 'admin-staffmanage.html', {"employees": employees})
            else:
                employees = Employee.objects.filter(s_id__contains=mohu)
                return render(request, 'admin-staffmanage.html', {"employees": employees})
        if condition=='3':
            if mohu == '':
                employees = Employee.objects.all()
                return render(request, 'admin-staffmanage.html', {"employees": employees})
            else:
                ment = Ment.objects.filter(ment_name__contains=mohu)
                if ment:
                    employees = ment[0].m.all()
                    return render(request, 'admin-staffmanage.html', {"employees": employees})
                else:
                    return render(request,'admin-staffmanage.html')
        if condition=='4':
            if mohu == '':
                employees = Employee.objects.all()
                return render(request, 'admin-staffmanage.html', {"employees": employees})
            else:
                employees = Employee.objects.filter(position__contains=mohu)
                return render(request, 'admin-staffmanage.html', {"employees": employees})
